package com.yyfax.ph.personal.message;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.aspsine.irecyclerview.IViewHolder;
import com.yyfax.common.recyclerview.BaseRecycleAdapter;
import com.yyfax.ph.R;
import com.yyfax.ph.base.YJPH;

import java.util.List;

public class MsgAdapter extends BaseRecycleAdapter<MsgModel.MsgInfo> {
	OnClickListener mListener;

	public MsgAdapter(OnClickListener listener) {
		mListener = listener;
	}

	public MsgAdapter(List<MsgModel.MsgInfo> data) {
		super(data);
	}

	@Override
	public void addData(MsgModel.MsgInfo data) {
		super.addData(data);
	}

	int i = 0;

	@Override
	public void onBindViewHolder(IViewHolder holder, int position) {
		final MsgModel.MsgInfo stationBean = getDataSet().get(position);
		MsgAdapter.ViewHolder view = (MsgAdapter.ViewHolder) holder;
		view.mInfoDateTv.setText(stationBean.createDate.length() >=5 ? stationBean.createDate.substring(5) : stationBean.createDate);
		view.mInfoTitleTv.setText(stationBean.title);
		view.mInfoContentTv.setText(stationBean.content);
		//如果可以查看详情
		if (MsgModel.TARGET_PROGRESS.equals(stationBean.target) || MsgModel.TARGET_SUBACCOUNT.equals(stationBean.target) || MsgModel.TARGET_CASH_DETAIL.equals(stationBean.target)) {
			view.mViewDetailsTv.setVisibility(View.VISIBLE);
			view.mItemView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mListener.onClick(stationBean);
				}
			});
		} else {
			//如果不能查看详情
			view.mViewDetailsTv.setVisibility(View.GONE);
			view.mItemView.setOnClickListener(null);
		}

		if (MsgModel.MESSAGE_UNREAD.equals(stationBean.status)) {
			Drawable drawable = YJPH.getInstance().getResources().getDrawable(R.drawable.ic_message_unread);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			view.mInfoTitleTv.setCompoundDrawables(drawable, null, null, null);
			view.mInfoTitleTv.setTextColor(YJPH.getInstance().color(R.color.color_333333));
			view.mInfoContentTv.setTextColor(YJPH.getInstance().color(R.color.color_262932));
			view.mInfoDateTv.setTextColor(YJPH.getInstance().color(R.color.color_666666));
			view.mViewDetailsTv.setTextColor(YJPH.getInstance().color(R.color.color_666666));
		} else {
			Drawable drawable = YJPH.getInstance().getResources().getDrawable(R.drawable.ic_message_read);
			drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
			view.mInfoTitleTv.setCompoundDrawables(drawable, null, null, null);
			view.mInfoTitleTv.setTextColor(YJPH.getInstance().color(R.color.color_999999));
			view.mInfoContentTv.setTextColor(YJPH.getInstance().color(R.color.color_999999));
			view.mInfoDateTv.setTextColor(YJPH.getInstance().color(R.color.color_999999));
			view.mViewDetailsTv.setTextColor(YJPH.getInstance().color(R.color.color_999999));
		}
	}

	@Override
	public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_item_layout, null);
		return new MsgAdapter.ViewHolder(view);
	}

	@Override
	public int getItemCount() {
		return getCount();
	}

	static class ViewHolder extends IViewHolder {
		@BindView(R.id.info_date_tv)
		TextView mInfoDateTv;
		@BindView(R.id.info_tile_tv)
		TextView mInfoTitleTv;
		@BindView(R.id.info_content_tv)
		TextView mInfoContentTv;

		@BindView(R.id.divider)
		View mDividerView;

		@BindView(R.id.view_detials_tv)
		TextView mViewDetailsTv;

		View mItemView;

		public ViewHolder(View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
			mItemView = itemView;
		}
	}

	interface OnClickListener {
		void onClick(MsgModel.MsgInfo msg);
	}

}
